<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="活动标题" prop="title">
        <el-input
            v-model="queryParams.title"
            placeholder="请输入活动标题"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="活动类型" prop="typeId">
        <el-select v-model="queryParams.typeId" placeholder="请选择活动类型" @keyup.enter="handleQuery" style="width: 150px">
          <el-option v-for="item in typeList" :key="item.value" :label="item.label" :value="item.value"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="难度等级" prop="difficulty">
        <el-select v-model="queryParams.difficulty" style="width: 150px" @keyup.enter="handleQuery" placeholder="请选择难度等级">
          <el-option label="新手" value="1"></el-option>
          <el-option label="进阶" value="2"></el-option>
          <el-option label="资深" value="3"></el-option>
          <el-option label="专家" value="4"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['nbot:activity:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="Edit"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['nbot:activity:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['nbot:activity:remove']"
        >删除
        </el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="activityList" @selection-change="handleSelectionChange">
      <el-table-column label="活动标题" prop="title"/>
      <el-table-column label="活动时间" prop="startAt" >
        <template #default="scope">
          <div>{{ scope.row.startAt }}</div>
          <div>{{ scope.row.endAt }}</div>
        </template>
      </el-table-column>
      <el-table-column label="总里程"prop="distance"/>
      <el-table-column label="活动地址"prop="address"/>
      <el-table-column label="活动封面" prop="image" width="100">
        <template #default="scope">
          <image-preview :src="scope.row.image" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="状态" prop="status"/>
      <el-table-column label="审核状态"  prop="auditStatus">
        <template #default="scope">
          <div>{{ scope.row.statusName }}</div>
          <div v-if="scope.row.auditStatus === 1 ">
            <el-button :disabled="scope.row.auditStatus !== 1" type="text" @click="PassAct(scope.row)">通过</el-button>
            <el-button :disabled="scope.row.auditStatus !== 1" type="text" @click="RejectAct(scope.row)">拒绝</el-button>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="报名列表" prop="createAt" >
        <template #default="scope">
          <el-button type="text" @click="OpenJoin(scope.row)">查看({{ scope.row.joinNum }})</el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作" class-name="small-padding fixed-width" width="140">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
                     v-hasPermi="['nbot:activity:edit']">修改
          </el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['nbot:activity:remove']">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改九号-活动信息对话框 -->
    <el-dialog :title="title" v-model="open"  width="800px" append-to-body>
      <el-form ref="activityRef" :model="form" :rules="rules" label-width="140px">
        <el-form-item label="活动标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入活动标题"/>
        </el-form-item>
        <el-form-item label="活动类型" prop="typeId">
          <el-select v-model="form.typeId" placeholder="请选择活动类型">
            <el-option v-for="item in typeList" :key="item.value" :label="item.label" :value="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="活动时间" prop="time">
          <el-date-picker
              time-format="HH:00"
              date-format="YYYY/MM/DD"
              format="YYYY/MM/DD HH:00"
              value-format="YYYY/MM/DD HH:mm"
              v-model="form.time"
              type="datetimerange"
              start-placeholder="开始时间"
              end-placeholder="结束时间"
          />
        </el-form-item>
        <el-form-item label="总里程" prop="distance">
          <el-input v-model="form.distance" type="number" placeholder="请输入总里程">
            <template #append>km</template>
          </el-input>
        </el-form-item>
        <el-form-item label="难度等级" prop="difficulty">
          <el-select v-model="form.difficulty" placeholder="请选择难度等级">
            <el-option label="新手" value="1"></el-option>
            <el-option label="进阶" value="2"></el-option>
            <el-option label="资深" value="3"></el-option>
            <el-option label="专家" value="4"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="活动地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入活动地址"/>
        </el-form-item>
        <el-form-item label="活动经度" prop="longitude">
          <el-input v-model="form.longitude" placeholder="请输入活动经度"/>
        </el-form-item>
        <el-form-item label="活动纬度" prop="latitude">
          <el-input v-model="form.latitude" placeholder="请输入活动纬度"/>
        </el-form-item>
        <el-form-item label="最大人数" prop="maxJoin">
          <el-input v-model="form.maxJoin" placeholder="请输入活动最大参与人数"/>
        </el-form-item>
        <el-form-item label="活动描述" prop="description">
          <Editor v-model="form.description" :height="300"/>
        </el-form-item>
        <el-form-item label="活动封面图" prop="image">
          <image-upload v-model="form.image" :limit="1" :file-size="2"/>
        </el-form-item>
<!--        <el-form-item label="活动费用(元)" prop="fee">-->
<!--          <el-input v-model="form.fee" placeholder="请输入活动费用(元)"/>-->
<!--        </el-form-item>-->
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <el-drawer
        v-model="showJoinData"
        title="报名列表"
        size="60%"
    >
        <el-form :model="RegQueryParams" ref="RegQueryForm" :inline="true" label-width="68px">
          <el-form-item label="真实姓名" prop="realName">
            <el-input
                v-model="RegQueryParams.realName"
                placeholder="请输入真实姓名"
                clearable
                @keyup.enter.native="handleRegQuery"
            />
          </el-form-item>
          <el-form-item label="联系电话" prop="phone">
            <el-input
                v-model="RegQueryParams.phone"
                placeholder="请输入联系电话"
                clearable
                @keyup.enter.native="handleRegQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="Search"  @click="handleRegQuery">搜索</el-button>
          </el-form-item>
        </el-form>
        <el-table :data="registrationsList" >
          <el-table-column label="真实姓名"  prop="realName" />
          <el-table-column label="联系电话"  prop="phone" />
          <el-table-column label="报名状态"  prop="registrationStatusName" />
          <el-table-column label="报名时间"  prop="registeredAt" />
          <el-table-column label="确认时间" prop="confirmedAt" />
          <el-table-column label="操作" class-name="small-padding fixed-width" width="140">
            <template #default="scope">
              <el-button :disabled="scope.row.registrationStatus !== 1" type="text" @click="Pass(scope.row)">通过</el-button>
              <el-button :disabled="scope.row.registrationStatus !== 1" type="text" @click="Reject(scope.row)">拒绝</el-button>
            </template>
          </el-table-column>
        </el-table>
    </el-drawer>
  </div>
</template>

<script setup name="Activity">
import {listActivity, getActivity, delActivity, addActivity, updateActivity} from "@/api/nbot/activity"
import {listType} from "@/api/nbot/actType.js";
import Editor from "@/components/Editor/index.vue";
import {listRegistrations, updateRegistrations} from "@/api/nbot/registrations.js";
import {ElMessageBox} from "element-plus";

const {proxy} = getCurrentInstance()
const activityList = ref([])
const registrationsList = ref([])
const typeList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")
const showJoinData = ref(false)





const difficultyList = reactive([{
  name: '新手',
  value: 1
},{
  name: '进阶',
  value: 2
},{
  name: '资深',
  value: 3
},{
  name: '专家',
  value: 4
}])

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    title: null,
    typeId: null,
    difficulty: null,
    status: null,
    auditStatus: null,
  },
  RegQueryParams: {
    realName: null,
    phone: null,
  },

  rules: {
    title: [
      {required: true, message: "活动标题不能为空", trigger: "blur"}
    ],
    typeId: [
      {required: true, message: "活动类型不能为空", trigger: "blur"}
    ],
    time:[
          {required: true, message: "活动时间不能为空", trigger: "blur"}
    ],
    distance: [
      {required: true, message: "总里程不能为空", trigger: "blur"}
    ],
    difficulty: [
      {required: true, message: "难度等级", trigger: "blur"}
    ],
    address: [
      {required: true, message: "活动地址不能为空", trigger: "blur"}
    ],
    maxJoin: [
      {required: true, message: "活动最大参与人数不能为空", trigger: "blur"}
    ],
    description: [
      {required: true, message: "活动描述不能为空", trigger: "blur"}
    ],
    image: [
      {required: true, message: "活动封面图不能为空", trigger: "blur"}
    ],
  }
})

const {queryParams,RegQueryParams, form, rules} = toRefs(data)

/** 查询九号-活动信息列表 */
function getList() {
  loading.value = true
  listActivity(queryParams.value).then(response => {
    activityList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

const PassAct = (row) => {
  ElMessageBox.confirm("是否确认通过该活动信息？", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "success"
  }).then(()=>{
    updateActivity({
      id: row.id,
      auditStatus: 2,
      status: 2
    }).then(response => {
      proxy.$modal.msgSuccess("审核通过")
      getList()
    })
  })
}


const RejectAct = (row) => {
  ElMessageBox.confirm("是否确认拒绝该活动信息？", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning"
  }).then(()=>{
    updateActivity({
      id: row.id,
      auditStatus: 3,
      status: 5
    }).then(response => {
      proxy.$modal.msgSuccess("审核拒绝")
      getList()
    })
  })
}

// 打开报名列表
const OpenJoin = (row) => {
  // 查询该活动的报名列表
  RegQueryParams.value.activityId = row.id
  listRegistrations(RegQueryParams.value).then(response => {
    registrationsList.value = response.data
    showJoinData.value = true
  })
}
const handleRegQuery = () => {
  listRegistrations(RegQueryParams.value).then(response => {
    registrationsList.value = response.data
  })
}

const getTypeList = () => {
  listType().then(response => {
    typeList.value = response.rows.map(item => {
      return {
        label: item.name,
        value: item.id
      }
    })
  })
}
// 通过审核
const Pass = (row) => {
  ElMessageBox.confirm("是否确认通过该报名信息？", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "success"
  }).then(()=>{
    updateRegistrations({
      id: row.id,
      registrationStatus: 2
    }).then(response => {
      proxy.$modal.msgSuccess("审核通过")
      handleRegQuery()
    })
  })
}

// 拒绝审核
const Reject = (row) => {
  ElMessageBox.confirm("是否确认拒绝该报名信息？", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning"
  }).then(()=>{
    updateRegistrations({
      id: row.id,
      registrationStatus: 3
    }).then(response => {
      proxy.$modal.msgSuccess("审核拒绝")
      handleRegQuery()
    })
  })
}


// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    title: null,
    typeId: null,
    time: null,
    startAt: null,
    endAt: null,
    distance: null,
    difficulty: null,
    address: null,
    longitude: null,
    latitude: null,
    maxJoin: null,
    description: null,
    image: null,
    fee: 0
  }
  proxy.resetForm("activityRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加活动信息"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _id = row.id || ids.value
  getActivity(_id).then(response => {
    form.value = response.data
    form.value.time = [response.data.startAt, response.data.endAt]
    open.value = true
    title.value = "修改活动信息"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["activityRef"].validate(valid => {
    if (valid) {
      form.value.startAt = form.value.time[0]
      form.value.endAt = form.value.time[1]
      if (form.value.id != null) {
        updateActivity(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addActivity(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal.confirm('是否确认删除活动信息编号为"' + _ids + '"的数据项？').then(function () {
    return delActivity(_ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {
  })
}


getList()
getTypeList()
</script>
